﻿

namespace Mulala.Cavan.Presentation.Mvc.Controller
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Web.Mvc;
    using Mulala.Cavan.Application.MainBoundedContext.AttachmentModule;
    using Mulala.Cavan.Application.MainBoundedContext.ConsignModule;
    using Mulala.Cavan.Application.MainBoundedContext.CorporationModule;
    using Mulala.Cavan.Application.MainBoundedContext.NannyModule;
    using Mulala.Cavan.Core.Extension;
    using Mulala.Cavan.Domain.MainBoundedContext.ConsignModule;
    using Mulala.Cavan.Domain.MainBoundedContext.CorporationModule;
    using Mulala.Cavan.Domain.MainBoundedContext.NannyModule;
    using Mulala.Cavan.Domain.MainBoundedContext.ProprietorModule;
    using Mulala.Cavan.Domain.MainBoundedContext.SearchModule;
    using Mulala.Cavan.Domain.MainBoundedContext.SecurityModule;
    using Mulala.Cavan.Domain.MainBoundedContext.Setting;
    using Mulala.Cavan.Presentation.Mvc.ViewModel;
    using Mulala.Cavan.Presentation.Seedwork;

    public class ConsignController : EntityController<Consign, ConsignSearch, IConsignViewModel, IConsignAppService>, IConsignController
    {
        private IAttachmentAppService AttachmentAppService { get; set; }
        private INannyAppService NannyAppService { get; set; }
        private ICorporationAppService CorporationAppService { get; set; }

        public ConsignController()
        {
            this.Model = new ConsignViewModel();
        }

        public override string ControllerName
        {
            get { return ControllerConst.CONSIGNCENTER; }
        }

        public override bool UseDenied
        {
            get { return false; }
        }

        [ActionName(ActionConst.LIST)]
        public ActionResult GetConsignsByCondition(ConsignSearch consignSearch)
        {
            long total = 0;
            IList<Consign> consigns = this.AppService.GetConsignsByCondition(consignSearch, out total);

            IList<IConsignViewModel> consignViewModels = ConsignViewModelConverter.Convert(consigns);

            return View(consignViewModels);
        }

        [ActionName(ActionConst.SEARCH)]
        public ActionResult GetConsignsByKeyword()
        {
            string keyword = this.HttpContext.Request["kw"].ToString();
            int currentPage = 1;
            int.TryParse(this.HttpContext.Request["cp"].ToString(), out currentPage);

            long total = 0;
            IList<Consign> consigns = this.AppService.GetConsignByKeyword(keyword, currentPage, out total);

            IList<IConsignViewModel> consignViewModels = ConsignViewModelConverter.Convert(consigns);

            return View(consignViewModels);
        }

        [ActionName(ActionConst.DETAIL)]
        public ActionResult GetConsign(int id)
        {
            Consign consign = this.AppService.GetById(id);

            ConsignViewModelConverter.Convert(consign, this.Model);
            this.Model.Avatar = this.AttachmentAppService.GetProprietorAvatar(consign.Proprietor);
            this.Model.Item = consign;

            return View(this.Model);
        }

        [ActionName(ActionConst.RECOMMENDCONSIGN)]
        [ChildActionOnly]
        [OutputCache(Duration = 3600)]
        public ActionResult GetConsignsByRecommend()
        {
            IDictionary<string, bool> orderBy = new Dictionary<string, bool>();
            orderBy.Add("CreateTime", true);

            IList<Consign> consigns = null;
            Nanny nanny = null;
            if (this.Model.MasterModel.CurrentUserModel.UserRole == SecurityIdentifier.Role.Corporation)
            {
                Corporation corporation = this.CorporationAppService.GetById(this.Model.MasterModel.CurrentUserModel.UserAccountID);
                nanny = corporation.Nannies.OrderByDescending(n => n.CreateTime).FirstOrDefault();
            }
            else
            {
                nanny = this.NannyAppService.GetById(this.Model.MasterModel.CurrentUserModel.UserAccountID);
            }
            ConsignSearch consignSearch = null;
            if (nanny.IsNotNull())
            {
                consignSearch = ConsignSearchFactory.CreateConsignSearch(nanny.Type, nanny.Experience, nanny.Area.ID, nanny.MainSkill.ToPositionArray(), orderBy, 1, Project.Settings.SystemRecommendConsignAmount);
                consigns = this.AppService.GetConsignsBySearch(consignSearch);
            }
            if (consigns.IsNull())
            {
                consignSearch = ConsignSearchFactory.CreateConsignSearch(true, orderBy, 1, Project.Settings.SystemRecommendConsignAmount);
                consigns = this.AppService.GetConsignsBySearch(consignSearch);
            }

            IConsignsViewModel consignsViewModel = ModelFactory.CreateModel<IConsignsViewModel>();
            IList<IConsignViewModel> consignViewModels = ConsignViewModelConverter.Convert(consigns);
            consignsViewModel.Consigns = consignViewModels;

            return PartialView("~/Views/Consign/_recommendConsignPartial.cshtml", consignsViewModel);
        }

        [ActionName(ActionConst.NANNYRECENTBIDDINGCONSIGN)]
        [ChildActionOnly]
        public ActionResult GetRecentBiddingConsignsByNanny()
        {
            INannyBiddingConsignViewModel nannyBiddingConsignViewModel = ModelFactory.CreateModel<INannyBiddingConsignViewModel>();
            IList<ConsignBidder> consignBidders = this.AppService.GetConsignsByNanny(this.Model.MasterModel.CurrentUserModel.UserAccountID);
            if (null != consignBidders && consignBidders.Count > 0)
            {
                consignBidders = consignBidders.OrderByDescending(cb => cb.CreateTime).Take(Project.Settings.RecentNannyBiddingConsign).ToList();
            }
            nannyBiddingConsignViewModel.ConsignBidders = ConsignBidderViewModelConverter.Convert(consignBidders);

            return PartialView("~/Views/Consign/_recentNannyBiddingConsignPartial.cshtml", nannyBiddingConsignViewModel);

        }

        [ActionName(ActionConst.CORPORATIONRECENTBIDDINGCONSIGN)]
        [ChildActionOnly]
        public ActionResult GetRecentBiddingConsignsByCorporation()
        {
            ICorporationBiddingConsignViewModel corporationBiddingConsignViewModel = ModelFactory.CreateModel<ICorporationBiddingConsignViewModel>();
            IList<ConsignBidder> consignBidders = this.AppService.GetConsignsByCorporation(this.Model.MasterModel.CurrentUserModel.UserAccountID);
            if (null != consignBidders && consignBidders.Count > 0)
            {
                consignBidders = consignBidders.OrderByDescending(cb => cb.CreateTime).Take(Project.Settings.RecentCorporationBiddingConsign).ToList();
            }
            corporationBiddingConsignViewModel.ConsignBidders = consignBidders != null ? ConsignBidderViewModelConverter.Convert(consignBidders) : new List<IConsignBidderViewModel>();

            return PartialView("~/Views/Consign/_recentCorporationBiddingConsignPartial.cshtml", corporationBiddingConsignViewModel);
        }

        [ActionName(ActionConst.PROPRIETORRECENTBIDDEDCONSIGN)]
        [ChildActionOnly]
        public ActionResult GetRecentBiddedConsignsByProprietor()
        {
            IProprietorBiddedConsignViewModel proprietorBiddedConsignViewModel = ModelFactory.CreateModel<IProprietorBiddedConsignViewModel>();
            IList<ConsignBidder> consignBidders = this.AppService.GetConsignBiddersByProprietor(this.Model.MasterModel.CurrentUserModel.UserAccountID);
            if (null != consignBidders && consignBidders.Count > 0)
            {
                consignBidders = consignBidders.OrderByDescending(cb => cb.CreateTime).Take(Project.Settings.RecentProprietorBiddedConsign).ToList();
            }
            proprietorBiddedConsignViewModel.ConsignBidders = ConsignBidderViewModelConverter.Convert(consignBidders);

            return PartialView("~/Views/Consign/_recentProprietorBiddedConsignPartial.cshtml", proprietorBiddedConsignViewModel);

        }
    }
}
